<?php

namespace apiend\modules\home\models;

use Yii;
use common\models\book\Book;

/**
 * 首页默认面板信息
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class HomeDashboardLogic extends \apiend\models\searchs\DashboardSearch {

    /**
     * @var array 统计块导航
     */
    public $menus = [];

    /**
     * @var array 轮播图
     */
    public $growup = [];

    /**
     * @var array 比赛
     */
    public $stars = [];

    public function setMenus() {
        return [];
        $model = new StoreStatsMenuLogic();
        return $model->search();
    }

    /**
     * 增长统计
     * @inheritdoc
     */
    protected function setGrowup() {
        return [];
        $orderQuery = ShopOrder::find();
        $orderQuery->andWhere([
            'store_id' => $this->store_id
        ]);
        $orderQuery->andWhere([
            'and',
            ['>', 'pay_time', 0],
            ['!=', 'trade_id', 0],
        ]);
        $nums = (int) $orderQuery->count();
        $amount = round($orderQuery->sum('pay'));

        $time = strtotime('today'); // $time = strtotime('2021-12-04');
        $todayQuery = clone $orderQuery;
        $todayQuery->andDuration('created_at', $time, $time + 2 * 86400 - 1);
        $todayQuery->select([
            'FROM_UNIXTIME(pay_time,"%Y-%m-%d") as date',
            'count(*) as nums',
            'sum(pay) as amount',
        ]);
        $todayQuery->groupBy([
            'date'
        ]);
        $days = $todayQuery->asArray()->all();
        //同比增长率=（本期数－同期数）/同期数×100%
        $temp = [
            'numbers' => [
                'name' => 'numbers',
                'text' => '订单数',
                'value' => 0,
                'yestoday' => 0,
                'amount' => $nums,
                'amountText' => '订单总数',
                'precision' => 0,
            ],
            'amounts' => [
                'name' => 'amounts',
                'text' => '销售额(元)',
                'value' => 0,
                'yestoday' => 0,
                'amount' => $amount,
                'amountText' => '销售总额',
                'precision' => 2,
            ],
            'collects' => [
                'name' => 'collects',
                'text' => '店铺收藏数',
                'value' => 0,
                'yestoday' => 0,
                'amount' => 0,
                'amountText' => '店铺收藏总数',
                'precision' => 0,
            ]
        ];
        foreach ($days as $day) {
            if (strtotime($day['date']) == $time) {
                $temp['numbers']['yestoday'] = (int) $day['nums'];
                $temp['amounts']['yestoday'] = round($day['amount']);
            } else {
                $temp['numbers']['value'] = (int) $day['nums'];
                $temp['amounts']['value'] = round($day['amount']);
            }
        }
        //
        $favQuery = UserShopFavorite::find()->andWhere([
            'store_id' => $this->store_id,
        ]);
        $temp['collects']['amount'] = $favQuery->count();
        $favQuery->andDuration('created_at', $time, $time + 2 * 86400 - 1);
        $favQuery->select([
            'FROM_UNIXTIME(created_at,"%Y-%m-%d") as date',
            'count(*) as nums',
        ]);
        $favQuery->groupBy([
            'date'
        ]);
        $collectDays = $favQuery->asArray()->all();
        foreach ($collectDays as $day) {
            if (strtotime($day['date']) == $time) {
                $temp['collects']['yestoday'] = (int) $day['nums'];
            } else {
                $temp['collects']['value'] = (int) $day['nums'];
            }
        }
        return array_values($temp);
    }

    /**
     * 店铺信息
     * @inheritdoc
     */
    protected function setStars() {
        return [];
    }

}
